package com.ruoyi.project.ypjxc.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysPost;
import com.ruoyi.project.ypjxc.domain.YFactory;
import com.ruoyi.project.ypjxc.service.YpjxcFactoryService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ypjxc/factory")
public class YpjxcFactoryController extends BaseController {
    @Autowired
    private YpjxcFactoryService factoryService;
    /**
     * 获取工厂列表
     */
    @PreAuthorize("@ss.hasPermi('ypjxc:factory:list')")
    @GetMapping("/list")
    public TableDataInfo list(YFactory factory) {
        startPage();
        List<YFactory> list = factoryService.selectYFactoryList(factory);
        return getDataTable(list);
    }

    /**
     * 新增工厂
     */
    @PreAuthorize("@ss.hasPermi('ypjxc:factory:add')")
    @Log(title = "工厂管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody YFactory factory) {
        if (factory==null){
            return AjaxResult.error("信息不能为空");
        }
        factory.setCreateBy(SecurityUtils.getUsername());
        return toAjax(factoryService.insertYFactory(factory));
    }

    /**
     * 修改工厂
     */
    @PreAuthorize("@ss.hasPermi('ypjxc:factory:edit')")
    @Log(title = "工厂管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody YFactory factory)
    {
        if (factory==null){
            return AjaxResult.error("信息不能为空");
        }
        factory.setCreateBy(SecurityUtils.getUsername());
        return toAjax(factoryService.updateYFactory(factory));
    }

    /**
     * 根据id查询
     * @param factoryId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('ypjxc:factory:query')")
    @GetMapping(value = "/{factoryId}")
    public AjaxResult getInfo(@PathVariable Long factoryId)
    {
        return AjaxResult.success(factoryService.selectYFactoryById(factoryId));
    }

    @PreAuthorize("@ss.hasPermi('ypjxc:factory:remove')")
    @Log(title = "工厂管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{factoryIds}")
    public AjaxResult delete(@PathVariable Long[] factoryIds){
        return toAjax(factoryService.deleteYFactoryByIds(factoryIds));
    }


}
